package cn.maxwell.pet.service.impl;

import cn.maxwell.basic.service.IBaiduAiAuditService;
import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.LoginContext;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.pet.domain.Pet;
import cn.maxwell.pet.domain.PetDetail;
import cn.maxwell.pet.domain.PetOnlineAuditLog;
import cn.maxwell.pet.mapper.PetDetailMapper;
import cn.maxwell.pet.mapper.PetMapper;
import cn.maxwell.pet.mapper.PetOnlineAuditLogMapper;
import cn.maxwell.pet.service.IPetService;
import cn.maxwell.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    /**
     * 添加并同操作宠物明细表
     * @param pet
     */
    @Override
    public void add(Pet pet) {
        //添加宠物信息到数据库t_pet表
        super.add(pet);

        // 获取添加的宠物自增主键id
        Long petId = pet.getId();
        // 获取宠物信息里面的宠物明细内容
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){//宠物明细不为空就操作添加方法，为空就不对数据库t_detail进行操作
            //设置t_detail表里面的pet_id值
            petDetail.setPet_id(petId);
            //添加宠物明细
            petDetailMapper.add(petDetail);
        }
    }

    /**
     * 修改并同时操作宠物明细表
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        super.update(pet);
        Long pet_id = pet.getId();
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPet_id(pet_id);
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    /**
     * 删除并同时操作宠物明细表
     * @param id
     */
    @Override
    public void delete(Long id) {
        super.delete(id);

        petDetailMapper.deleteByPetId(id);
    }

    /**
     * 上架+审核
     * @param ids
     * @param request
     */
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        /*//获取登录人信息
        Logininfo logininfo = (Logininfo)LoginContext.getLoginUser(request);
        //获取管理员信息,emplo表里面的logininfo_id与logininfo登录信息里面的id相对应。通过登录信息id可以查询出管理员id
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        Long employeeId = employee.getId();
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //1.审核（百度AI）
            Boolean nameBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imgBoo = baiduAiAuditService.imageAudit(pet.getResources());
            if(!nameBoo && imgBoo) {
                //2.审核通过-->修改状态（注意不要修改创建时间）
                //2.1 设置上架时间
                pet.setOnsaletime(new Date());
                //2.2 设置状态
                pet.setState(1);
                //2.3 对数据库进行修改
                petMapper.update(pet);
                //3. +添加上架成功日志信息
                PetOnlineAuditLog onsaleLog = new PetOnlineAuditLog();

                onsaleLog.setPet_id(pet.getId());
                onsaleLog.setState(1);
                onsaleLog.setNote("上架成功");
                onsaleLog.setAudit_time(new Date());
                onsaleLog.setAudit_id(employeeId);

                petOnlineAuditLogMapper.add(onsaleLog);
            }else {
                //3.审核不通过-->添加上架失败日志信息
                PetOnlineAuditLog onsaleLog = new PetOnlineAuditLog();

                onsaleLog.setPet_id(pet.getId());
                onsaleLog.setState(-1);
                onsaleLog.setNote("审核失败");
                onsaleLog.setAudit_time(new Date());
                onsaleLog.setAudit_id(employeeId);

                petOnlineAuditLogMapper.add(onsaleLog);
            }
        }*/
        for (Long id : ids ) {
            //根据id查询pet对象
            Pet pet = petMapper.loadById(id);
            //1.获取登录人信息
            Logininfo logininfo = (Logininfo) LoginContext.getLoginUser(request);
            //1.1 获取登录信息id
            Long logininfo_id = logininfo.getId();
            //1.2 根据logininfo_id查询用户信息：当前是后台管理（employee表）
            Employee employee = employeeMapper.loadByLogininfoId(logininfo_id);


            //2.审核通过-->修改状态（注意不要修改创建时间）,设置相关字段
            //2.1. 设置状态
            pet.setState(1);
            //2.2 设置上架时间
            pet.setOnsaletime(new Date());
            //3.更新数据
            petMapper.update(pet);

            //3.添加日志信息
            PetOnlineAuditLog petLog = new PetOnlineAuditLog();
            //设置宠物id
            petLog.setPet_id(pet.getId());
            //设置操作人员字段
            petLog.setAudit_id(employee.getId());
            //设置日志修改时间
            petLog.setAudit_time(new Date());
            //设置状态
            petLog.setState(1);
            //设置审核备注
            petLog.setNote("审核成功!");

            //更新到数据库
            petOnlineAuditLogMapper.add(petLog);
        }

    }

    /**
     * 下架（无需审核）
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //下架时间（new一个当前系统时间）
        Date offsaletime = new Date();
        //多参数：@param：将下架时间和所要操作的id传到数据库的操作方法
        petMapper.offsale(offsaletime,ids);
    }
}
